using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Entities;
using BUS;
using Utility;

public partial class Admin_SanPham_CapNhatSanPham : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadDropDownList();
            LoadData();
            LoadHinh();
        }
    }

    #region"LoadData"
    private void LoadData()
    {
        try
        {
            int id = int.Parse(Request.QueryString["id"]);

            tbSanPham sanPham = tbSanPhamBUS.GetByID(id);
            lit_MaSP.Text = id.ToString();
            txt_TenSP.Text = sanPham.TenSP;

            ddl_DanhMuc.SelectedValue = sanPham.DanhMuc.ToString();

            ddl_DoanhNghiep.SelectedValue = sanPham.DoanhNghiep.ToString();

          
            txt_GiaGoc.Text = sanPham.GiaGoc.ToString();
            txt_GiaBan.Text = sanPham.GiaBan.ToString();
            txt_NgayBatDau.Text = sanPham.NgayBatDau.ToShortDateString();
            txt_NgayHetHan.Text = sanPham.NgayHetHan.ToShortDateString();
            txt_SoLuongBan.Text = sanPham.SoLuongBan.ToString();
            txt_SoLuongToiDa.Text = sanPham.SoLuongToiDa.ToString();
            ftb_MoTa.Text = sanPham.MoTa;
           ftb_QuyDinhSP.Text = sanPham.QuyDinhSuDung;
            txt_ChietKhau.Text = sanPham.ChietKhau.ToString();

            ddl_TinhTrangSP.SelectedValue = sanPham.TinhTrangSP.ToString();
            ddl_NhanVien.SelectedValue = sanPham.NhanVien.ToString();
            txt_DiaDiemSuDung.Text = sanPham.DiaDiemSuDung;

            if (sanPham.BanDo != "no_images.jpg")
            {
                img_BanDo.ImageUrl =
                "~/Photos/BanDo/Thumb/" + sanPham.BanDo;
            }
            else
            {
                img_BanDo.ImageUrl = "~/Photos/Thumb/no_images.jpg";
            }


        }
        catch
        {
            Response.Redirect("~/Admin/SanPham/DanhSachSanPham.aspx");
        }

    }
    #endregion
    #region"LoadHinh"
    private void LoadHinh()
    {
        int id = int.Parse(Request.QueryString["id"]);
        int total = 0;
        pagerHinh.PageSize = 2;
        Criteria c = new Criteria();
        c.add(Condition.AND, tbHinhAnhColumns.MaSP.ToString(), Condition.EQUAL, id);

        tbHinhAnhCollection cthinhAnhCollection = tbHinhAnhBUS.Get(c.Criter, pagerHinh.CurrentIndex, pagerHinh.PageSize, tbHinhAnhColumns.MaHA.ToString(), "ASC", out total);
        Repeater1.DataSource = cthinhAnhCollection;
        Repeater1.DataBind();
        if (Repeater1.Items.Count == 0)
        {
            img_no.Visible = true;
        }
        else
        {
            img_no.Visible = false;
        }
        pagerHinh.ItemCount = total;
    }
    #endregion
    #region"LoadDropDownList"
    public void LoadDropDownList()
    {

        tbDanhMucCollection danhMucCollection = tbDanhMucBUS.GetALL();
        ddl_DanhMuc.DataTextField = tbDanhMucColumns.TenDanhMuc.ToString();
        ddl_DanhMuc.DataValueField = tbDanhMucColumns.MaDM.ToString();
        ddl_DanhMuc.DataSource = danhMucCollection;
        ddl_DanhMuc.DataBind();


        tbDoanhNghiepCollection danhNghiepCollection = tbDoanhNghiepBUS.GetALL();
        ddl_DoanhNghiep.DataTextField = tbDoanhNghiepColumns.TenDN.ToString();
        ddl_DoanhNghiep.DataValueField = tbDoanhNghiepColumns.MaDN.ToString();
        ddl_DoanhNghiep.DataSource = danhNghiepCollection;
        ddl_DoanhNghiep.DataBind();


      


        tbTinhTrangSPCollection tinhTrangSPCollection = tbTinhTrangSPBUS.GetAll();
        ddl_TinhTrangSP.DataTextField = tbTinhTrangSPColumns.TenTT.ToString();
        ddl_TinhTrangSP.DataValueField = tbTinhTrangSPColumns.MaTT.ToString();
        ddl_TinhTrangSP.DataSource = tinhTrangSPCollection;
        ddl_TinhTrangSP.DataBind();


        tbNhanVienCollection nhanVienCollection = tbNhanVienBUS.GetALL();
        ddl_NhanVien.DataTextField = tbNhanVienColumns.TenNV.ToString();
        ddl_NhanVien.DataValueField = tbNhanVienColumns.MaNV.ToString();
        ddl_NhanVien.DataSource = nhanVienCollection;
        ddl_NhanVien.DataBind();

    }
       #endregion
    protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
    {

    }
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem ||
        e.Item.ItemType == ListItemType.Item)
        {
            tbHinhAnh ctHinhAnh = (tbHinhAnh)e.Item.DataItem;

            Image img_HinhAnh = (Image)e.Item.FindControl("img_HinhAnh");
          
            img_HinhAnh.ImageUrl = "~/Photos/Thumb/" + ctHinhAnh.Link;


        }
    }
    protected void lbt_Xoa_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < Repeater1.Items.Count; i++)
        {
        CheckBox cb_Xoa = (CheckBox)Repeater1.Items[i].FindControl("cb_Xoa");
        if (cb_Xoa.Checked == true)
        {
            HiddenField hf_id = (HiddenField)Repeater1.Items[i].FindControl("hf_id");
            int id = int.Parse(hf_id.Value);
                tbHinhAnh hinhAnh = tbHinhAnhBUS.GetByID(id);

                System.IO.File.Delete(Server.MapPath("~/Photos/" + hinhAnh.Link));
                System.IO.File.Delete(Server.MapPath("~/Photos/Thumb/" + hinhAnh.Link));
                tbHinhAnhBUS.Delete(hinhAnh.MaHA);


            }
        }
       
        Response.Redirect(Request.RawUrl);
    }
    protected void btn_Add_Click(object sender, EventArgs e)
    {
        int id = int.Parse(Request.QueryString["id"]);
        tbSanPham sanPham = tbSanPhamBUS.GetByID(id) ;
        sanPham.MaSP = id;
        sanPham.TenSP = txt_TenSP.Text;
        sanPham.DanhMuc = int.Parse(ddl_DanhMuc.SelectedValue);
        sanPham.DoanhNghiep = int.Parse(ddl_DoanhNghiep.SelectedValue);
     
        if (txt_GiaGoc.Text == "")
            txt_GiaGoc.Text = "0";
        sanPham.GiaGoc = int.Parse(txt_GiaGoc.Text);

        if (txt_GiaBan.Text == "")
            txt_GiaBan.Text = "0";
        sanPham.GiaBan = int.Parse(txt_GiaBan.Text);


        if (txt_NgayBatDau.Text == "")
        {
            sanPham.NgayBatDau = DateTime.Now;
        }
        else
        {
            sanPham.NgayBatDau = DateTime.Parse(txt_NgayBatDau.Text);
        }


        if (txt_NgayHetHan.Text == "")
        {
            sanPham.NgayHetHan = DateTime.Now;
        }
        else
        {
            sanPham.NgayHetHan = DateTime.Parse(txt_NgayHetHan.Text);

        }

        if (txt_SoLuongBan.Text == "")
            txt_SoLuongBan.Text = "0";
        sanPham.SoLuongBan = int.Parse(txt_SoLuongBan.Text);

        if (txt_SoLuongToiDa.Text == "")
            txt_SoLuongToiDa.Text = "0";
        sanPham.SoLuongToiDa = int.Parse(txt_SoLuongToiDa.Text);


        sanPham.MoTa = ftb_MoTa.Text;
        sanPham.QuyDinhSuDung = ftb_QuyDinhSP.Text;

        if (txt_ChietKhau.Text == "")
        {
            txt_ChietKhau.Text = "0";
        }
        sanPham.ChietKhau = int.Parse(txt_ChietKhau.Text);

        sanPham.TinhTrangSP = int.Parse(ddl_TinhTrangSP.SelectedValue);
        sanPham.NhanVien = int.Parse(ddl_NhanVien.SelectedValue);
        sanPham.DiaDiemSuDung=txt_DiaDiemSuDung.Text;

        if (fu_BanDo.HasFile)
        {
            string filename = DateTime.Now.Ticks.ToString() + "_" + fu_BanDo.FileName;
            fu_BanDo.SaveAs(Server.MapPath("~/Photos/BanDo/" + filename));

            System.Drawing.Image img = System.Drawing.Image.FromFile(
                Server.MapPath("~/Photos/BanDo/" + filename));

            ImageUtility.MakeThumbnailPhoto(
                img, 100, 100, Server.MapPath("~/Photos/BanDo/Thumb/"), filename);

          

            if (sanPham.BanDo != "no_images.jpg")
            {
                System.IO.File.Delete(Server.MapPath("~/Photos/BanDo/" + sanPham.BanDo));
                System.IO.File.Delete(Server.MapPath("~/Photos/BanDo/Thumb/" + sanPham.BanDo));
            }
            sanPham.BanDo = filename;
        }
       
        tbSanPhamBUS.Update(sanPham);

        if (Request.Files.Count == 1)
        {

        }
        else
        {
            for (int i = 1; i < Request.Files.Count-1; i++)
            {
                HttpPostedFile file = Request.Files[i];
                tbHinhAnh hinh = new tbHinhAnh();
                if (!String.IsNullOrEmpty(file.FileName))
                {
                    string filename = DateTime.Now.Ticks.ToString() + "_" + file.FileName;
                    file.SaveAs(Server.MapPath("~/Photos/" + filename));
                    System.Drawing.Image img = System.Drawing.Image.FromFile(
                    Server.MapPath("~/Photos/" + filename));

                    ImageUtility.MakeThumbnailPhoto(
                        img, 100, 100, Server.MapPath("~/Photos/Thumb/"), filename);

                    hinh.Hinh = sanPham.TenSP;
                    hinh.Link = filename;
                    hinh.MaSP = id;
                    int mahinh;
                    mahinh = tbHinhAnhBUS.Insert(hinh);
       
                    

                }
            }
        }

        Response.Redirect("~/Admin/SanPham/DanhSachSanPham.aspx");
    }
    protected void pagerHinh_Command(object sender, CommandEventArgs e)
    {
        pagerHinh.CurrentIndex = int.Parse(e.CommandArgument.ToString());
        LoadHinh();
    }
}
